<?php 
class AdminAction extends Action {
	public function index(){
		$model=M('admin_user');
		$list=$model->select();
		
		
		$this->assign('list',$list);
		$this->display();
	}
	
	public function add_index(){
		$this->display();
	}
	
	public function add(){
		header('Content-Type:text/html; charset=utf-8');
		if ($_POST['submit']){
			//$map=array();
			$_POST['username']=trim($_POST['name']);
			$_POST['password']=md5(trim($_POST['password'])."&abc");
			$_POST['email']=trim($_POST['email']);
			$_POST['authority']=trim($_POST['quanxian']);
			$_POST['caret_date']=time();
	
			
		$model = M('admin_user');
		$vo=$model->create();
		if ($vo) {
            if (false !== $model->add()) {
                $this->redirect('Admin/index', array(),1,'<script language="JavaScript">alert("添加成功")</script>');
            } else {
                $this->error("添加失败");
            }
        } else {
            header("Content-Type:text/html; charset=utf-8");
            $this->error($model->getError().' [ <a href="javascript:history.back()">return</a> ]');
            exit();
        }
			
		}
		
		$this->display();
	}
	
	public function edit_index(){
		if($_GET['id']){
			$id=$_GET['id'];
			$model2=M('admin_user');
			$model2->getById($id);
			$name=$model2->username;
			$emial=$model2->email;
			//$pwd=$model2->password;
			$quanxian=$model2->authority;
		}
		
		if($quanxian==10){
			$value="超级管理员";
		}elseif ($quanxian==9){
			$value="管理员";
		}elseif ($quanxian==2){
			$value="物流管理";
		}elseif ($quanxian==1){
			$value="客服";
		}
		
		$this->assign('id',$id);
		$this->assign('value',$value);
		$this->assign('name',$name);
		$this->assign('email',$emial);
		//$this->assign('password',$pwd);
		$this->assign('quanxian',$quanxian);
		$this->display();
	}
	
	public function edit(){
	if ($_POST['submit']){
			$whereid=trim($_POST['id']);
			$map=array();
			$model1=M('admin_user');
			$model1->getById($_POST['id']);
			$name=$model1->username;
			$emial=$model1->email;
			$quanxian=$model1->authority;
			$pwd=$model1->password;
			unset($_POST['id']);
			if($name!=trim($_POST['name'])){
				$map['username']=trim($_POST['name']);
			}
			if(trim($_POST['password'])!=""){
				$map['password']=md5(trim($_POST['password'])."&abc");
			}
			if($emial!=trim($_POST['email'])){
				$map['email']=trim($_POST['email']);
			}
			if($quanxian!=trim($_POST['quanxian'])){
				$map['authority']=trim($_POST['quanxian']);
			}
			//$map['create']=time();
			
			
			$model=M('admin_user');
			if (false !==$model->where('id='.$whereid)->save($map)){
				$this->redirect('Admin/index', array(),1,'<script language="JavaScript">alert("修改成功")</script>');
			}else {
                $this->error("修改失败");
            }	
		}
	}
	
	public function del_index(){
		$this->display();
	}
}
?>